import { mainRoutes } from "@/router";
import { IMenuType } from "@/types/router";
import { cloneDeep } from "lodash-es";
export const filterMenu = (permission: string[]) => {
  let routes = cloneDeep(mainRoutes); //深度克隆数据包
  function loop(arr: IMenuType[]) {
    for (let i = arr.length - 1; i >= 0; i--) {
      if (arr[i].children) {
        let permitArr = permission.filter(
          (path) => path.indexOf(arr[i].key) != -1
        );
        if (permitArr.length) {
          //当前有children的父级的key，在permission中有没有类似
          loop(arr[i].children as IMenuType[]);
        } else {
          arr.splice(i, 1);
        }
      } else {
        let bool = !permission.includes(arr[i].key);
        if (bool) {
          arr.splice(i, 1);
        }
      }
    }
  }
  loop(routes);
  return routes;
};
